//
//  AddressEditViewController.m
//  YYPHSpeedJump
//
//  Created by sujiang on 16/1/7.
//  Copyright © 2016年 sujiang. All rights reserved.
//

#import "AddressEditViewController.h"
#import "HZAreaPickerView.h"
#import "BaseTextField.h"
@interface AddressEditViewController ()<HZAreaPickerDelegate, UITextFieldDelegate>

@property (strong, nonatomic) NSString *areaValue;
@property (strong, nonatomic) HZAreaPickerView *locatePicker;
@property (strong, nonatomic) BaseTextField *detailAdd;

@end


@implementation AddressEditViewController

- (void)loadView
{
    self.view = [[AddressEditView alloc] initWithFrame:[UIScreen mainScreen].bounds];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"编辑地址";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:(UIBarButtonItemStylePlain) target:self action:@selector(actionSave)];
    self.view.backgroundColor = kColors(233, 233, 233, 1);
    AddressEditView *view = (AddressEditView *)self.view;
    if (self.model != nil) {
        view.phoneTF.text = self.model.phone;
        view.postCodeTF.text = self.model.zipcode;
        view.receiverTF.text = self.model.real_name;
        view.addressTF.text = self.model.address;
        
    }
    view.addressTF.delegate = self;
    self.detailAdd = [[BaseTextField alloc] initWithFrame:CGRectMake(0, view.addressTF.bottom + 1, kScreenWidth, 88)];
    [view addSubview:self.detailAdd];
    self.detailAdd.placeholder = @"详细地址";
    self.detailAdd.textContainerInset = UIEdgeInsetsMake(15, 10, 0, 0);
}

-(void)setAreaValue:(NSString *)areaValue
{
    AddressEditView *view = (AddressEditView *)self.view;
    _areaValue = areaValue;
    view.addressTF.text = areaValue;
}

#pragma mark - HZAreaPicker delegate
-(void)pickerDidChaneStatus:(HZAreaPickerView *)picker
{
    if (picker.pickerStyle == HZAreaPickerWithStateAndCityAndDistrict) {
        self.areaValue = [NSString stringWithFormat:@"%@ %@ %@", picker.locate.state, picker.locate.city, picker.locate.district];
    }
}

-(void)cancelLocatePicker
{
    [self.locatePicker cancelPicker];
    self.locatePicker.delegate = nil;
    self.locatePicker = nil;
}


#pragma mark - TextField delegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    [self.view endEditing:YES];
    AddressEditView *view = (AddressEditView *)self.view;
    if ([textField isEqual:view.addressTF]) {
        [self cancelLocatePicker];
        self.locatePicker = [[HZAreaPickerView alloc] initWithStyle:HZAreaPickerWithStateAndCityAndDistrict delegate:self];
        [self.locatePicker showInView:self.view];
    }
    return NO;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    [self cancelLocatePicker];
    [self.view endEditing:YES];
}


#pragma mark -- 按钮点击事件
- (void)actionSave
{
    // 保存
    AddressEditView *view = (AddressEditView *)self.view;
    //
    NSDictionary *dic;
    NSString *string;
    if ([self.judg isEqualToString:@"add"]) {
        string = [NSString stringWithFormat:@"%@%@", kBaseUrl, kAddressAddr];
        dic = @{@"u":[[NSUserDefaults standardUserDefaults] objectForKey:@"user_id"], @"area":view.addressTF.text,@"address":self.detailAdd.text, @"zipcode":view.postCodeTF.text, @"phone":view.phoneTF.text, @"real_name":view.receiverTF.text};
    } else if ([self.judg isEqualToString:@"edit"]) {
        string = [NSString stringWithFormat:@"%@%@", kBaseUrl,kAddressEdit];
        dic = @{@"u":[[NSUserDefaults standardUserDefaults] objectForKey:@"user_id"],@"area":view.addressTF.text, @"address":self.detailAdd.text, @"zipcode":view.postCodeTF.text, @"phone":view.phoneTF.text, @"real_name":view.receiverTF.text, @"address_id":self.model.address_id};
    }
    [NetworkHandle getInfoWithUrl:string dictionary:dic success:^(NSDictionary *dictionary) {
        // NSLog(@"%@", dictionary);
        if ([dictionary[@"code"] isEqualToString:@"200"]) {
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            [Tools showAlert:dictionary[@"message"]];
        }
    }];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
